package com.ruoyi.caigou.service.impl;

import com.ruoyi.caigou.domain.BuyOrderGoods;
import com.ruoyi.caigou.mapper.BuyOrderGoodsMapper;
import com.ruoyi.caigou.service.IBuyOrderGoodsService;
import com.ruoyi.system.api.RemoteStockInDetailService;
import com.ruoyi.system.api.RemoteStockInService;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.system.api.domain.store.StockInDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 采购订单商品Service业务层处理
 *
 * @author lvql
 * @date 2024-11-02
 */
@Slf4j
@Service
public class BuyOrderGoodsServiceImpl implements IBuyOrderGoodsService {
    @Autowired
    private BuyOrderGoodsMapper buyOrderGoodsMapper;

    @Autowired
    private RemoteStockInService remoteStockInService;
    @Autowired
    private RemoteStockInDetailService remoteStockInDetailService;

    /**
     * 查询采购订单商品
     *
     * @param id 采购订单商品主键
     * @return 采购订单商品
     */
    @Override
    public BuyOrderGoods selectBuyOrderGoodsById(Integer id) {
        return buyOrderGoodsMapper.selectBuyOrderGoodsById(id);
    }

    /**
     * 查询采购订单商品列表
     *
     * @param buyOrderGoods 采购订单商品
     * @return 采购订单商品
     */
    @Override
    public List<BuyOrderGoods> selectBuyOrderGoodsList(BuyOrderGoods buyOrderGoods) {
        return buyOrderGoodsMapper.selectBuyOrderGoodsList(buyOrderGoods);
    }

    /**
     * 新增采购订单商品
     *
     * @param buyOrderGoods 采购订单商品
     * @return 结果
     */
    @Override
    public int insertBuyOrderGoods(BuyOrderGoods buyOrderGoods) {
        return buyOrderGoodsMapper.insertBuyOrderGoods(buyOrderGoods);
    }

    /**
     * 修改采购订单商品
     *
     * @param buyOrderGoods 采购订单商品
     * @return 结果
     */
    @Override
    public int updateBuyOrderGoods(BuyOrderGoods buyOrderGoods) {
        return buyOrderGoodsMapper.updateBuyOrderGoods(buyOrderGoods);
    }

    /**
     * 批量删除采购订单商品
     *
     * @param ids 需要删除的采购订单商品主键
     * @return 结果
     */
    @Override
    public int deleteBuyOrderGoodsByIds(Integer[] ids) {
        return buyOrderGoodsMapper.deleteBuyOrderGoodsByIds(ids);
    }

    /**
     * 删除采购订单商品信息
     *
     * @param id 采购订单商品主键
     * @return 结果
     */
    @Override
    public int deleteBuyOrderGoodsById(Integer id) {
        return buyOrderGoodsMapper.deleteBuyOrderGoodsById(id);
    }

    @Override
    public List<BuyOrderGoods> useSelectBuyOrderGoodsList(BuyOrderGoods buyOrderGoods) {
        log.info("BuyOrderGoodsServiceImpl useSelectBuyOrderGoodsList params:{}", buyOrderGoods);
        //先查询退货详情，获取详情列表，再根据编号查询入库单，由入库单查询对应的详情
        List<BuyOrderGoods> buyOrderGoodsList = buyOrderGoodsMapper.selectBuyOrderGoodsList(buyOrderGoods);
        StockIn stockIn = new StockIn();
        stockIn.setOrderCode(buyOrderGoods.getBuyOrderCode());
        List<StockIn> stockIns = remoteStockInService.getStockList(stockIn);
        //如果申请单有对应绑定的退货订单就继续
        if (stockIns.size() == 0 || stockIns == null) {
            return buyOrderGoodsList;
        }
        // 获取 stockInId 数组
        // 将 List 转换为数组
        Integer[] idArray = stockIns.stream().map(StockIn::getId).toArray(Integer[]::new);
        List<StockInDetail> stockInDetails = remoteStockInDetailService.getStockInDetail(idArray);
        //如果有详情就继续
        if (stockInDetails.isEmpty()) {
            return buyOrderGoodsList;
        }
        //最终结果
        List<BuyOrderGoods> buyOrderGoodsList1 = new ArrayList<>();
        for (BuyOrderGoods buyOrderGoods1 : buyOrderGoodsList) {
            int proNum = buyOrderGoods1.getPurchaseQuantity();
            for (StockInDetail stockInDetail : stockInDetails) {
                if (buyOrderGoods1.getGoodsId().equals(stockInDetail.getGoodsId())) {
                    proNum -= stockInDetail.getTotalNum();
                }
            }
            if (proNum > 0) {
                buyOrderGoods1.setPurchaseQuantity(proNum);
                buyOrderGoodsList1.add(buyOrderGoods1);
            }
        }
        return buyOrderGoodsList1;

    }
}
